#include "widget.h"
//进度条 弹窗能不能在中间跳出
//准备增加image绘图部分
Widget::Widget(QWidget *parent) :
    QWidget(parent){
    initialForm();
}
Widget::~Widget(){
}
void Widget::initialForm(){
    mainForm = new MainForm(this);
    mUniqueID = new uniqueID(this);
    mCalibration = new calibration(this);
    mCoordinate = new coordinate(this);
    mCommonData = new commonData(this);
    mExplanation = new explanation(this);
    mSerial = new Serial(this);
    mImportFile = nullptr;
    mainForm->SetMainForm(this);//主界面初始化
    //屏蔽主界面三个摁键的功能
    mainForm->setStructEnabled(false);
    mainForm->setCoeffEnabled(false);
    mainForm->setExplanEnabled(false);
    //decode增加元素 目前只有两个功能 一个标定 一个精度验证
    empowerPushButton();
}
//建立信号与槽函数
void Widget::empowerPushButton(){
    if( mainForm == nullptr){
        return;
    }
    //连接
    connect(mainForm->getConnectButtonPointer(),&QPushButton::clicked,this,&Widget::chooseConnectModel);
    //结构参数
    connect(mainForm->getStructButtonPointer(),&QPushButton::clicked,this,&Widget::readStructSlots);
    //偏心误差
    connect(mainForm->getCoeffButtonPointer(),&QPushButton::clicked,this,&Widget::readCoeffSlots);
    //说明
    connect(mainForm->getExplanButtonPointer(),&QPushButton::clicked,mExplanation,&explanation::showUi);
    //前界面三个部分进行赋值
    connect(mUniqueID,&uniqueID::formButtonEnable,[=](){
        mainForm->setStructEnabled(true);
        mainForm->setCoeffEnabled(true);
        mainForm->setExplanEnabled(true);
    }
    );
}
void Widget::readStructSlots(){
    if(mImportFile == nullptr){
        mImportFile = new importStructFile(this);
    }
    else{
        delete mImportFile;
        mImportFile = new importStructFile(this);
    }
    bool result = mImportFile->importFileMain(false,commonData::structureType);
    if(result){
        QString coffInfo = QString("%1%2%3").arg("结构参数导入成功(").arg(commonData::SizeOFStruct).arg("项）");
        QMessageBox::about(this,tr("参数导入提醒"),coffInfo);
    }
    else {
        QMessageBox::about(this,tr("参数导入提醒"),tr("结构参数导入失败！"));
    }
}
void Widget::readCoeffSlots(){
    if(mImportFile == nullptr){
        mImportFile = new importCoeffFile(this);
    }
    else{
        delete mImportFile;
        mImportFile = new importCoeffFile(this);
    }
    bool result = mImportFile->importFileMain(false,commonData::coeffType);
    if(result){
        QString coffInfo = QString("%1%2%3").arg("偏心误差导入成功(").arg(mCommonData->getCoeffParameterSize()).arg("项）");
        QMessageBox::about(this,tr("参数导入提醒"),coffInfo);
    }
    else {
        QMessageBox::about(this,tr("参数导入提醒"),tr("偏心误差导入失败！"));
    }
}
void Widget::chooseConnectModel(){
    QMessageBox box(QMessageBox::Warning, "连接方式选择","请连接方式选择：");
    box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    box.setButtonText(QMessageBox::Yes, QString("Normal"));
    box.setButtonText(QMessageBox::No, QString("Unique ID"));
    int ret = box.exec();

    if(ret == QMessageBox::Yes){
        decodeModelChoose();
        mUniqueID->manulConnect();
    }
    else if(ret == QMessageBox::No){
        mUniqueID->uniqueIDMain();
    }
}
void Widget::decodeModelChoose(){
    QMessageBox chooseBox(QMessageBox::Warning, "正常模式","请选择通讯位数：");
    chooseBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    chooseBox.setButtonText(QMessageBox::Yes, QString("26位"));
    chooseBox.setButtonText(QMessageBox::No, QString("50位"));
    int chooseRet = chooseBox.exec();
    if(chooseRet == QMessageBox::Yes){
        //26位
        mCommonData->setFlagFor50Bytes(false);
        mCommonData->setFlagFor26Bytes(true);
    }
    else if(chooseRet == QMessageBox::No){
        //50位
        mCommonData->setFlagFor50Bytes(true);
        mCommonData->setFlagFor26Bytes(false);
    }
}
void Widget::paintEvent(QPaintEvent *event){
    Q_UNUSED(event);
    QPainter p(this);//指定对象为窗口
    p.drawPixmap(0,0,width(),height(),QPixmap("://image/uk.png"));
}
void Widget::closeEvent(QCloseEvent *event){
    int ret = QMessageBox::question(this,"关闭提示","是否关闭窗口？",QMessageBox::Yes,QMessageBox::No);
    if(ret == QMessageBox::Yes){
        mCalibration->dropImageThread();
        event->accept();
    }
    else if(ret == QMessageBox::No){
        event->ignore();
    }
}
void Widget::initialMethod(){
    if(mCommonData->getCoeffParameterSize() == 0||mCommonData->getCoeffParameterItem(0) == 0){
        QMessageBox::about(this,"提示","偏心参数需要重新重新导入");
    }
    if(mCommonData->getStructParameterItem(0) == 0){
        QMessageBox::about(this,"提示","结构参数需要重新重新导入");
    }
}
void Widget::setMethod(Method * arg){
    Decode::removeAll();//清空队列
    Decode::addMethodObject(arg);//添加
    initialMethod();
//    arg->assigAbstractObject(arg);
    arg->pretreatment();//UI界面展示等等
}
//添加特定的观察者
bool Widget::eventFilter(QObject *obj, QEvent *event){
    if( mCommonData->getLabelEnable() == false ){
        return false;
    }
    if ( mainForm && mainForm->isCalibPartEnable(obj) ){
        if (event->type() == QEvent::MouseButtonPress){
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            if(mouseEvent->button() == Qt::LeftButton){
                setMethod(mCalibration);
                mCalibration->assigAbstractObject(mCalibration);
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }
    else if( mainForm->isCoordiPartEnable(obj) ){
        if (event->type() == QEvent::MouseButtonPress){
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            if(mouseEvent->button() == Qt::LeftButton){
                setMethod(mCoordinate);
                mCoordinate->assigAbstractObject(mCoordinate);
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }
    return false;
}
